<template>
    <el-dialog
            v-model="visible"
            :title="title"
            :width="isMobile ? '90%' : '400px'"
            :modal="true"
            :show-close="false"
            :align-center="true"
            custom-class="warning-dialog"
    >
        <div class="dialog-body">
            <el-icon color="#E6A23C" size="24">
                <WarningFilled />
            </el-icon>
            <div class="message">{{ message }}</div>
        </div>

        <template #footer>
            <div class="footer-btn">
                <el-button
                        :type="isMobile ? 'primary' : 'warning'"
                        size="default"
                        @click="closeDialog"
                >
                    我知道了
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup>
    import { ref, watch, onMounted } from 'vue'
    import { WarningFilled } from '@element-plus/icons-vue'

    // 是否移动端
    const isMobile = ref(false)
    onMounted(() => {
        isMobile.value = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent)
    })

    // 组件 props
    const props = defineProps({
        title: { type: String, default: '提示' },
        message: { type: String, default: '' },
        modelValue: { type: Boolean, default: false }
    })

    // 向父组件同步显示状态
    const emit = defineEmits(['update:modelValue'])
    const visible = ref(props.modelValue)

    function closeDialog() {
        visible.value = false
        emit('update:modelValue', false)
    }

    watch(
        () => props.modelValue,
        (val) => (visible.value = val)
    )
</script>

<style scoped>
    .warning-dialog .el-dialog__body {
        padding: 16px;
    }
    .dialog-body {
        display: flex;
        align-items: center;
        gap: 12px;
        font-size: 15px;
        color: #e6a23c;
        word-break: break-word;
        text-align: left;
    }
    .footer-btn {
        display: flex;
        justify-content: center;
    }
    @media (max-width: 768px) {
        .dialog-body {
            font-size: 14px;
        }
        .footer-btn .el-button {
            width: 100%;
        }
    }
</style>
